package com.cyys.modules.core.sysParam;

import com.cyys.common.redis.RedisUtil;
import com.cyys.modules.system.model.user.User;
import com.cyys.modules.system.service.user.UserService;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class UserUtil {

	public static void init(UserService userService) {
		if(RedisUtil.hSize(RedisConstants.USER_MAP_FULL) >= 0){
			RedisUtil.delete(RedisConstants.USER_MAP_FULL);
		}
		List<User> list = userService.listAll(null);
		for (User entry : list) {
			RedisUtil.hPut(RedisConstants.USER_MAP_FULL,entry.getId(),entry);
		}
	}

	public static Map<Object, Object> getAll() {
		return RedisUtil.hGetAll(RedisConstants.USER_MAP_FULL);
	}

	public static User get(String userId) {
		return (User)RedisUtil.hGet(RedisConstants.USER_MAP_FULL, userId);
	}

	public static void add(User user) {
		RedisUtil.hPut(RedisConstants.USER_MAP_FULL,user.getId(),user);
	}

	public static void delete(String userId) {
		RedisUtil.hDelete(RedisConstants.USER_MAP_FULL,userId);
	}

	public static void modify(User user) {
		RedisUtil.hDelete(RedisConstants.USER_MAP_FULL,user.getId());
		RedisUtil.hPut(RedisConstants.USER_MAP_FULL,user.getId(),user);
	}
}
